package com.ruoyi.station.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.station.domain.SOpticalCable;
import com.ruoyi.station.domain.SStation;
import com.ruoyi.station.mapper.SOpticalCableMapper;
import com.ruoyi.station.mapper.SStationMapper;
import com.ruoyi.station.service.SOpticalCableService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 光缆Service业务层处理
 *
 * @author ruoyi
 * @date 2021-08-24
 */
@Slf4j
@Service
public class SOpticalCableServiceImpl extends ServiceImpl<SOpticalCableMapper, SOpticalCable> implements SOpticalCableService {
    @Autowired
    private SOpticalCableMapper sOpticalCableMapper;
    @Autowired
    private SStationMapper sStationMapper;
    /**
     * 导入光缆数据
     * @param file
     * @return
     */
    @Override
    @Transactional
    public String importsDistributionFrames(MultipartFile file) {
        String username = SecurityUtils.getUsername();
        List<List<Object>> lists = FileUtils.parseUploadFile(file);
        int i = 3;
        for (List<Object> readOptical : lists) {
            //光缆名称
            String name = String.valueOf(readOptical.get(1)).trim();
            if(StringUtils.isBlank(name)) return "第" + i +"行光缆名称不正确！";

            //所属区域
            String area = DictUtils.getDictValue("area", String.valueOf(readOptical.get(2)).trim());
            if(StringUtils.isBlank(area)) return "第" + i +"行所属区域不存在！";
            //类型
            String opticalCableType = DictUtils.getDictValue("Cable_type", String.valueOf(readOptical.get(3)).trim());
            if(StringUtils.isBlank(opticalCableType)) return "第" + i +"行类型不存在！";

            //长度
            String length = String.valueOf(readOptical.get(4)).trim();
            if(StringUtils.isBlank(length)) return "第" + i +"行长度不正确！";

            //起点资源类型
            String startSourceName = String.valueOf(readOptical.get(5)).trim();
            SStation startStation = sStationMapper.selectOne(new LambdaQueryWrapper<SStation>().eq(SStation::getStationName, startSourceName));
            if (startStation == null ) return "第" + i +"行起点资源类型不存在！";
            //起点资源
            String startSource = String.valueOf(readOptical.get(6)).trim();
            if(StringUtils.isBlank(startSource)) return "第" + i +"行起点资源不正确！";

            //终点资源类型
            String endSourceName = String.valueOf(readOptical.get(7)).trim();
            SStation endStation = sStationMapper.selectOne(new LambdaQueryWrapper<SStation>().eq(SStation::getStationName, endSourceName));
            if (endStation == null ) return "第" + i +"行终点资源类型不存在！";
            //终点资源
            String endSource = String.valueOf(readOptical.get(8)).trim();
            if(StringUtils.isBlank(endSource)) return "第" + i +"行终点资源不正确！";

            //产权单位
            String propertyUnit = String.valueOf(readOptical.get(9)).trim();
            if(StringUtils.isBlank(propertyUnit)) return "第" + i +"行产权单位不正确！";

            SOpticalCable build = SOpticalCable.builder()
                    .name(name)
                    .area(area)
                    .opticalCableType(opticalCableType)
                    .length(length)
                    .startSourceType(startStation.getId())
                    .startSource(startSource)
                    .endSourceType(endStation.getId())
                    .endSource(endSource)
                    .propertyUnit(propertyUnit)
                    .createBy(username)
                    .createTime(DateUtil.date())
                    .build();
            int insert = sOpticalCableMapper.insert(build);
            if (insert < 1) return "第" + i +"行保存出错了！";
            i++;
        }
        return "成功导入"+lists.size()+"条";
    }

}
